//
//  AY_SearchResultView.m
//  AiYue
//
//  Created by mac on 2023/11/10.
//

#import "AY_SearchResultView.h"
#import "AY_SearchCell.h"
#import "AY_SearchEmptyView.h"
#import "AY_BookDetailViewController.h"
#import "AY_BookQiuBookPage.h"

@interface AY_SearchResultView ()<UITableViewDataSource,UITableViewDelegate>

/**
 UITableView 基类
 */
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *books;
@property (nonatomic,strong) NSString *searchKey;
@property (nonatomic, strong) AY_SearchEmptyView * emptyView;
@property (nonatomic,strong) UIButton *qiushuView;
@end

@implementation AY_SearchResultView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.tableView];
        [self addSubview:self.qiushuView];
    }
    return self;
}

#pragma mark -
- (void)qiushuAction {
    AY_BookQiuBookPage * page = [[AY_BookQiuBookPage alloc] init];
    page.bookName = self.searchKey;
    [[AYTool viewControllerWith:self].navigationController pushViewController:page animated:YES];
}

- (void)setConfigModel:(ConfigModel *)configModel {
    self.emptyView.configModel = configModel;
}

- (void)sendBooks:(NSMutableArray *)books andSearchKey:(NSString *)searchKey {
    self.books = [NSMutableArray arrayWithArray:books];
    self.searchKey = searchKey;
    if(kArrayIsEmpty(self.books)) {
        [self addSubview:self.emptyView];
        self.emptyView.titleLabel.text = searchKey;
    }else {
        if(self.emptyView) {
            [self.emptyView removeFromSuperview];
        }
    }
    [self.tableView reloadData];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [[AYTool viewControllerWith:self].view endEditing:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return section == 0 ? self.books.count : 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        static NSString *ID = @"AY_SearchCell";
        AY_SearchCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[AY_SearchCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell sendModel:self.books[indexPath.row] andSearchKey:self.searchKey];
        return cell;
    }else {
        static NSString *ID = @"UITableViewCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.section == 0 ? YNUM(123) : YNUM(50);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        AY_BookDetailViewController *bookDetailVC = [[AY_BookDetailViewController alloc] init];
        bookDetailVC.bookId = [self.books[indexPath.row] bookId];
        [[AYTool viewControllerWith:self].navigationController pushViewController:bookDetailVC animated:YES];
    }
}

- (void)dismiss {
    [self.books removeAllObjects];
    [self.tableView reloadData];
    [self removeFromSuperview];
}

- (void)loadingInView:(UIView *)view {
    [view addSubview:self];
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        UITableView *tableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.showsVerticalScrollIndicator = NO;
        tableView.dataSource = self;
        tableView.translatesAutoresizingMaskIntoConstraints = NO;
        tableView.backgroundColor = [UIColor whiteColor];
        tableView.delegate = self;
        tableView.tableFooterView = [[UIView alloc] init];
        _tableView = tableView;
    }
    return _tableView;
}

- (UIButton *)qiushuView {
    if (!_qiushuView) {
        _qiushuView = [AYTool createButtonWithFrame:CGRectMake(YNUM(60), SCREEN_H - GK_TABBAR_HEIGHT - GK_STATUSBAR_NAVBAR_HEIGHT + YNUM(20), SCREEN_W - YNUM(120), YNUM(35)) andTitle:@"没有搜到?告诉我们你想找的书吧" andFont:YFont(14) andTitleColor:[UIColor whiteColor] andBg:THEME_COLOR andImage:nil andBgImage:nil andTarget:self andAction:@selector(qiushuAction) andRadius:YNUM(18) andAddSubView:self];
        UIImageView *arrow = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"AY_arrow_white") andBg:nil andRadius:0 andAddSubView:_qiushuView];
        [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(YNUM(15));
            make.centerY.equalTo(_qiushuView);
            make.right.equalTo(_qiushuView).offset(-YNUM(10));
        }];
    }
    return _qiushuView;
}

- (AY_SearchEmptyView *)emptyView {
    if(!_emptyView) {
        AY_SearchEmptyView * emptyView = [[AY_SearchEmptyView alloc] initWithFrame:self.bounds];
        emptyView.backgroundColor = COLOR(@"#F6F7FB");
        _emptyView = emptyView;
    }
    return _emptyView;
}

@end
